package com.yzyz.management.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.yzyz.bean.book.BookBean;
import com.yzyz.service.IBookService;
import com.yzyz.utils.BookStatus;
import com.yzyz.utils.ValidateUtils;
import com.yzyz.utils.json.processor.DateProcessor;
import com.yzyz.utils.error.Error;
import com.yzyz.utils.error.ErrorBean;

/**
 * 管理员管理行程预订信息
 * @author david.sun
 * @date 2010-7-12
 */
public class BookController extends MultiActionController {
	
	private IBookService bookService;

	public void setBookService(IBookService bookService) {
		this.bookService = bookService;
	}
	
	/**
	 * 更新预订信息的处理状态
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int bookId = Integer.valueOf(request.getParameter("bookId"));
		BookBean book = (BookBean)this.bookService.find(bookId);
		if(book == null) {
			this.writeToClient(response, ErrorBean.getInstance(Error.CAN_NOT_FIND_OBJECT).toJson());
			
		} else {
			book.setStatus(BookStatus.getStatus(Integer.valueOf(request.getParameter("status"))));
			this.bookService.update(book);
			this.writeToClient(response, ErrorBean.getSuccessMsg());
		}
		
	}
	
	/**
	 * 删除预订信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int bookId = Integer.valueOf(request.getParameter("bookId"));
		int result = this.bookService.delete(bookId);
		if(result != Error.SUCCESS) {
			this.writeToClient(response, ErrorBean.getInstance(result).toJson());
		} else {
			this.writeToClient(response, ErrorBean.getSuccessMsg());
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public void paginate(HttpServletRequest request, HttpServletResponse response) throws IOException{
		int start = 0;
		int limit = 15;
		if(!ValidateUtils.isNull(request.getParameter("start"))) {
			start = Integer.valueOf(request.getParameter("start"));
		}
		List<BookBean> list = (List<BookBean>)this.bookService.paginate(start, limit);
		if(list == null) {
			list = new ArrayList<BookBean>(0);
		}
		
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class, new DateProcessor());
		JSONArray arr = JSONArray.fromObject(list, config);
		StringBuffer sb 			= new StringBuffer();
		sb.append("{\"sum\":").append("\""+this.bookService.getCount()+"\",");
		sb.append("\"data\":").append(arr.toString());
		sb.append("}");
		this.writeToClient(response, sb.toString());
		
	}
	
	private void writeToClient(HttpServletResponse response, String contents) throws IOException {
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Programa", "no-cache");
		response.setDateHeader("Expires", -1L);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(contents);
		response.flushBuffer();
		response.getWriter().close();
	}
}
